package com.habdqn.studentmanager.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;

import com.github.pagehelper.PageInfo;
import com.habdqn.studentmanager.bean.ajax.Page;
import com.habdqn.studentmanager.bean.ajax.Request;
import com.habdqn.studentmanager.bean.ajax.Response;
import com.habdqn.studentmanager.bean.ajax.RowsResponse;
import com.habdqn.studentmanager.constant.Constant;
import com.habdqn.studentmanager.exception.StudentException;

/**
 * @author Fan
 * 
 *         2017年6月29日
 * 
 *         辅助控制器类
 */
public abstract class BaseController {

	protected static final Logger logger = LoggerFactory.getLogger(BaseController.class);
	
    /**
     * 设置视图变量
     * 
     * @param model
     * @param key
     * @param value
     */
    protected void addAttr(Model model, String key, Object value) {
        model.addAttribute(key, value);
    }

    /**
     * 设置错误提示
     * 
     * @param model
     * @param msg
     *            错误提示
     * @return 并返回错误页面地址
     */
    protected String setErrMsg(Model model, String msg) {
        model.addAttribute(Constant.ERROR_MSG, msg);
        return Constant.ERROR_PAGE;
    }

    /**
     * 设置错误提示
     * 
     * @param model
     * @param e
     *            业务异常
     */
    protected String setErrMsg(Model model, StudentException e) {
        model.addAttribute(Constant.ERROR_MSG, e.getMessage());
        return Constant.ERROR_PAGE;
    }

    /**
     * 构建响应消息
     * 
     * @return
     */
    protected Response buildRsp() {
        Response rsp = new Response();
        return rsp;
    }

    /**
     * 构建响应消息
     * 
     * @param data
     * @return
     */
    protected Response buildRsp(Object obj) {
        Response rsp = new Response();
        rsp.setData(obj);
        return rsp;
    }

    /**
     * 构建响应消息
     * 
     * @param errCode
     * @param msg
     * @return
     */
    protected Response buildRsp(String errCode, String msg) {
        Response rsp = new Response();
        rsp.setErr(errCode);
        rsp.setMsg(msg);
        return rsp;
    }

    /**
     * 构建响应消息
     * 
     * @param errCode
     * @param e
     * @return
     */
    protected Response buildRsp(String errCode, StudentException e) {
        Response rsp = new Response();
        rsp.setErr(errCode);
        rsp.setMsg(e.getMessage());
        return rsp;
    }

    /**
     * 构建带有集合的响应消息
     * 
     * @param total
     *            总条数
     * @param rows
     *            当前分页的记录
     * @return
     */
    protected Response buildRsp(long total, List<?> rows) {
        RowsResponse rsp = new RowsResponse();
        rsp.setTotal(total);
        rsp.setRows(rows);
        return rsp;
    } 
    
    /**
     * 将pageInfo转换成响应消息
     * @param pageInfo
     * @return
     */
    protected Response buildRsp(PageInfo<?> pageInfo) {
        RowsResponse rsp = new RowsResponse();
        Page page = Page.valueOf(pageInfo);
        rsp.setRows(pageInfo.getList());
        rsp.setPage(page);
        return rsp;
    }
    
    /**
     * 从请求中获取分页对象, 如果为空, 则返回默认的
     * @param req
     * @return
     */
    protected Page getPage(Request<?> req) {
    	
    	Page page = req.getPage();
    	
    	if (null == page) {
    		return new Page();
    	}
    	
    	page.reset();
    	return page;
    }
    
    /**
     * 设置下载模式
     * @param rsp
     * @param fileName
     * @return
     */
    protected OutputStream setDownLoadModel(HttpServletResponse rsp, String fileName) {
        
        // 设置强制下载不打开
        rsp.setContentType("application/force-download");
        // 设置文件名
        rsp.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
        
        OutputStream out = null;
        try {
            out = rsp.getOutputStream();
        }
        catch (IOException e) {
            logger.debug("打开输出流失败", e);
            throw new StudentException("请求失败", e);
        }
        
        return out;
    }
}
